package com.shangjinrong.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangjinrong.domain.UserAccount;
import com.shangjinrong.domain.UserInfo;
import com.shangjinrong.domain.UserLoginRecord;
import com.shangjinrong.domain.dto.LoginDto;
import com.shangjinrong.domain.dto.UserInfoQuery;
import com.shangjinrong.domain.vo.UserIndexVO;
import com.shangjinrong.domain.vo.UserInfoVO;
import com.shangjinrong.mapper.UserAccountMapper;
import com.shangjinrong.mapper.UserInfoMapper;
import com.shangjinrong.mapper.UserLoginRecordMapper;
import com.shangjinrong.service.UserInfoService;
import com.shangjinrong.service.UserLoginRecordService;
import com.shangjinrong.util.DateUtils;
import com.shangjinrong.util.JwtUtils;
import com.shangjinrong.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserLoginRecordService userLoginRecordService;
    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserLoginRecordMapper userLoginRecordMapper;


    @Override
    public UserInfo selectByMobile(String mobile) {
        return this.userInfoMapper.selectByMobile(mobile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserInfoVO login(LoginDto loginDto) {
        loginDto.setPassword(MD5.encrypt(loginDto.getPassword()));
        UserInfo login = this.userInfoMapper.login(loginDto.getMobile(), loginDto.getPassword());
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setUserId(login.getId());
        userLoginRecord.setIp(loginDto.getIp());
        userLoginRecordService.save(userLoginRecord);
        //生成token
        String token = JwtUtils.createToken(login.getId(), login.getName());
        //组装UserInfoVO
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setToken(token);
        userInfoVO.setName(login.getName());
        userInfoVO.setMobile(login.getMobile());
        userInfoVO.setHeadImg(login.getHeadImg());
        userInfoVO.setNickName(login.getNickName());
        userInfoVO.setUserType(login.getUserType().intValue());
        return userInfoVO;

    }

    @Override
    public void paging(Page page, UserInfoQuery query) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(query.getMobile() != null, UserInfo::getMobile, query.getMobile())
                .eq(query.getUserType() != null, UserInfo::getUserType, query.getUserType())
                .eq(query.getStatus() != null, UserInfo::getStatus, query.getStatus());
        baseMapper.selectPage(page, wrapper);
    }

    @Override
    public UserIndexVO getIndexUserInfo(long userId) {

        //用户信息
        UserInfo userInfo = baseMapper.selectById(userId);
        //账户信息
        QueryWrapper<UserAccount> userAccountQueryWrapper = new QueryWrapper<>();
        userAccountQueryWrapper.eq("user_id", userId);
        UserAccount userAccount = userAccountMapper.selectOne(userAccountQueryWrapper);
        //登录日志
        QueryWrapper<UserLoginRecord> userLoginRecordQueryWrapper = new QueryWrapper<>();
        userLoginRecordQueryWrapper.eq("user_id", userId).orderByDesc("id").last("limit 1");
        UserLoginRecord userLoginRecord = userLoginRecordMapper.selectOne(userLoginRecordQueryWrapper);


        //组装结果对象
        UserIndexVO userIndexVO = new UserIndexVO();
        userIndexVO.setUserId(userId);
        userIndexVO.setUserType(userInfo.getUserType().intValue());
        userIndexVO.setName(userInfo.getName());
        userIndexVO.setHeadImg(userInfo.getHeadImg());
        userIndexVO.setNickName(userInfo.getNickName());
        userIndexVO.setBindStatus(userInfo.getBindStatus().intValue());
        userIndexVO.setAmount(userAccount.getAmount());
        userIndexVO.setFreezeAmount(userAccount.getFreezeAmount());
        userIndexVO.setLastLoginTime(DateUtils.data2LocalDateTime(userLoginRecord.getCreateTime()));
        return userIndexVO;
    }

    @Override
    public IPage<UserInfo> listPage(Page<UserInfo> pageParam, UserInfoQuery userInfoQuery) {
        String mobile = userInfoQuery.getMobile();
        Integer status = userInfoQuery.getStatus();
        Integer userType = userInfoQuery.getUserType();

        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();

        if (userInfoQuery == null) {
            return baseMapper.selectPage(pageParam, null);
        }

        userInfoQueryWrapper
                .eq(StringUtils.isNotBlank(mobile), "mobile", mobile)
                .eq(status != null, "status", userInfoQuery.getStatus())
                .eq(userType != null, "user_type", userType);
        return baseMapper.selectPage(pageParam, userInfoQueryWrapper);
    }

    @Override
    public void lock(Long id, Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setStatus(status.byteValue());
        baseMapper.updateById(userInfo);
    }
}
